package com.ruoyi.syrw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.report_tg.mapper.TgsybgBaDetailMapper;
import com.ruoyi.sycs.service.IBiQualificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.syrw.domain.bo.YssyjcbgBsInfoBo;
import com.ruoyi.syrw.domain.vo.YssyjcbgBsInfoVo;
import com.ruoyi.syrw.domain.YssyjcbgBsInfo;
import com.ruoyi.syrw.mapper.YssyjcbgBsInfoMapper;
import com.ruoyi.syrw.service.IYssyjcbgBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 岩石试验检测报告Service业务层处理
 *
 * @author xisen
 * @date 2024-05-11
 */
@RequiredArgsConstructor
@Service
public class YssyjcbgBsInfoServiceImpl implements IYssyjcbgBsInfoService {

    private final YssyjcbgBsInfoMapper baseMapper;
    private final TgsybgBaDetailMapper detailMapper;

    private final IBiQualificationService biQualificationService;

    /**
     * 查询岩石试验检测报告
     */
    @Override
    @FIllBasicInfo
    public YssyjcbgBsInfoVo queryById(Long excelId){
        YssyjcbgBsInfoVo vo = baseMapper.selectVoById(excelId);
        List<TgsybgBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId));
        baDetailVos.forEach(v->{
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
        Map<Long, TgsybgBaDetailVo> collect = baDetailVos.stream().collect(Collectors.toMap(TgsybgBaDetailVo::getQualId, e -> e));
        vo.setBaDetailVos(collect);
        return vo;
    }

    /**
     * 查询岩石试验检测报告列表
     */
    @Override
    public TableDataInfo<YssyjcbgBsInfoVo> queryPageList(YssyjcbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<YssyjcbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<YssyjcbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询岩石试验检测报告列表
     */
    @Override
    public List<YssyjcbgBsInfoVo> queryList(YssyjcbgBsInfoBo bo) {
        LambdaQueryWrapper<YssyjcbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<YssyjcbgBsInfo> buildQueryWrapper(YssyjcbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<YssyjcbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), YssyjcbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), YssyjcbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), YssyjcbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), YssyjcbgBsInfo::getYangpxx, bo.getYangpxx());
        return lqw;
    }

    /**
     * 新增岩石试验检测报告
     */
    @Override
    @Transactional
    public Boolean insertByBo(YssyjcbgBsInfoBo bo) {
        YssyjcbgBsInfo add = BeanUtil.toBean(bo, YssyjcbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            long key = 154l;
            detailMapper.insert(new TgsybgBaDetail(add.getExcelId(),key));
        }
        return flag;
    }

    /**
     * 修改岩石试验检测报告
     */
    @Override
    @Transactional
    @ComputeReportResult
    public Boolean updateByBo(YssyjcbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        YssyjcbgBsInfo update = BeanUtil.toBean(bo, YssyjcbgBsInfo.class);
        validEntityBeforeSave(update);
        detailMapper.insertOrUpdateBatch(BeanUtil.copyToList(bo.getBaDetailVos().values(), TgsybgBaDetail.class));
        return baseMapper.updateById(update) > 0;
    }

    private void fillExcelResult(YssyjcbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos().values()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
            return;
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }



    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(YssyjcbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除岩石试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
